package product;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class CodeInput extends JInternalFrame {

	HashMap<String, CodeData> cod;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JTextField proCode;
	private JTextField proName;
	private JTextField proPrice;
	private JButton btnNewButton;
	private JLabel status;
	/**
	 * Create the frame.
	 */
	public CodeInput(HashMap<String, CodeData> c) {
		super("CodeInput", true, true, true, false);
		this.cod = c;
		setBounds(100, 100, 230, 180);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getProCode());
		getContentPane().add(getProName());
		getContentPane().add(getProPrice());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getStatus());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품코드");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("제품명");
			lblNewLabel_1.setBounds(12, 35, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("단가");
			lblNewLabel_2.setBounds(12, 60, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getProCode() {
		if (proCode == null) {
			proCode = new JTextField();
			proCode.setBounds(81, 7, 116, 21);
			proCode.setColumns(10);
		}
		return proCode;
	}
	private JTextField getProName() {
		if (proName == null) {
			proName = new JTextField();
			proName.setBounds(81, 32, 116, 21);
			proName.setColumns(10);
		}
		return proName;
	}
	private JTextField getProPrice() {
		if (proPrice == null) {
			proPrice = new JTextField();
			proPrice.setHorizontalAlignment(SwingConstants.RIGHT);
			proPrice.setBounds(81, 57, 116, 21);
			proPrice.setColumns(10);
		}
		return proPrice;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("추가");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String code = proCode.getText();
						String name = proName.getText();
						int price = Integer.parseInt(proPrice.getText());

						if (cod.get(code) == null) {
							boolean flag = true;
							if (("").equals(code)) {
								status.setText("코드를 입력하세요");
								flag = false;
							} else if (name.equals("")) {
								status.setText("제품명을 입력하세요");
								flag = false;
							} else {
								try {
									if (proPrice.getText().equals("")) {
										status.setText("단가를 입력하세요");
										flag = false;
									}
								} catch (NumberFormatException ne) {
									JOptionPane.showMessageDialog(null,
											"단가에는 숫자를 입력해주세요");
								}
							}
							if (flag) {
								CodeData data = new CodeData(code, name, price);
								cod.put(code, data); // 저장
								FileOutputStream fw = new FileOutputStream("CodeList.dat");
								ObjectOutputStream bw = new ObjectOutputStream(fw);
								bw.writeObject(cod);
								bw.close();
								fw.close();
								System.out.println("저장성공");
								status.setText(code + "저장됨");
								proCode.setText("");
								proName.setText("");
								proPrice.setText("");
							}
						} else {
							JOptionPane.showMessageDialog(null, "코드가 겹칩니다.");
						}
					}  catch (Exception e) {
						System.out.println("error");
					}
				}
			});
			btnNewButton.setBounds(81, 88, 116, 23);
		}
		return btnNewButton;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setBackground(new Color(0, 255, 255));
			status.setOpaque(true);
			status.setBounds(12, 121, 185, 15);
		}
		return status;
	}
}
